﻿using AdverseEventReportSystem.BLL;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Service;
using AdverseEventReportSystem.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AdverseEventReportSystem.Web.Areas.AdverseEvent
{
    public class AdverseEventListController : BaseController
    {
        // GET: AdverseEvent/AdverseEventList
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 事件列表
        /// </summary>
        /// <returns></returns>
        public ActionResult Search()
        {
            int pageIndex = DataConvert.StrToInt(Request["page"], 1);
            int pageSize = DataConvert.StrToInt(Request["pageSize"], ConfigHelper.GetConfigInt("pagesize"));
            string whereSql = GetWhereSql();
            SelectBuilder data = new SelectBuilder();
            data.Having = "";
            data.GroupBy = "";
            data.OrderBy = "ID DESC";
            data.From = "AdverseEvent";
            data.Select = "ID,EventTypeName,NurseName,AreaName,OccurrTime,CreateTime,Status";
            data.WhereSql = whereSql;
            data.PagingCurrentPage = pageIndex;
            data.PagingItemsPerPage = pageSize;
            int total = 0;
            DataTable dt = BaseService<Model.AdverseEvent>.GetQueryManyForPage_Proc(data, out total);
            var result = new { Total = total, Rows = dt };
            return JsonDate(result);
        }

        /// <summary>
        /// 获取查询条件
        /// </summary>
        /// <returns></returns>
        private string GetWhereSql()
        {
            int year = DataConvert.StrToInt(Request["year"]);
            int quarter = DataConvert.StrToInt(Request["quarter"]);
            int month = DataConvert.StrToInt(Request["month"]);
            int eventTypeID = DataConvert.StrToInt(Request["eventTypeID"]);

            string whereSql = "CreateUserID = '" + loginUser.Personnel_id + "' and (Flag_Yc is null or Flag_Yc = 0)";
            if (quarter == 0)
            {
                whereSql += string.Format(" and CreateTime >= '{0}' and CreateTime < '{1}'", new DateTime(year, 1, 1).ToString(), new DateTime(year, 1, 1).AddYears(1).ToString());
            }
            else
            {
                if (month == 0)
                {
                    whereSql += string.Format(" and CreateTime >= '{0}' and CreateTime < '{1}'", new DateTime(year, quarter * 3 - 2, 1).ToString(), new DateTime(year, quarter * 3 - 2, 1).AddMonths(3).ToString());
                }
                else
                {
                    whereSql += string.Format(" and CreateTime >= '{0}' and CreateTime < '{1}'", new DateTime(year, month, 1).ToString(), new DateTime(year, month, 1).AddMonths(1).ToString());
                }
            }
            if (eventTypeID > 0)
            {
                whereSql += string.Format(" and EventTypeID = {0}", eventTypeID);
            }

            return whereSql;
        }

        /// <summary>
        /// 获取事件类型
        /// </summary>
        /// <param name="isAll">是否包含一个全部选项</param>
        /// <returns></returns>
        public ActionResult GetAdverseEventTypeList(int isAll = 0)
        {
            DataTable dt = AdverseEventTypeService.GetAdverseEventTypeTable();
            if (isAll > 0)
            {
                DataRow dr = dt.NewRow();
                dr["ID"] = 0;
                dr["Name"] = "全部";
                dt.Rows.InsertAt(dr, 0);
            }
            return JsonDate(dt);
        }

        /// <summary>
        /// 删除不良事件
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(int id)
        {
            OperateResult resultInfo = AdverseEventManager.Delte(id);
            return JsonDate(resultInfo);
        }
    }
}